<template>
  <el-container class="app-container">
    <!-- 侧边栏 -->
    <el-aside width="250px" class="sidebar">
      <div class="logo">
        <h2>Trainer后台管理系统</h2>
      </div>
      <el-menu
        :default-active="$route.path"
        class="sidebar-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/">
          <i class="el-icon-s-home"></i>
          <span>仪表盘</span>
        </el-menu-item>
        
        <el-menu-item index="/questions">
          <i class="el-icon-document"></i>
          <span>题目管理</span>
        </el-menu-item>
        
        <el-submenu index="user-management">
          <template slot="title">
            <i class="el-icon-user"></i>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/students">学员管理</el-menu-item>
          <el-menu-item index="/admins">管理员管理</el-menu-item>
        </el-submenu>
        
        <el-submenu index="analytics">
          <template slot="title">
            <i class="el-icon-s-data"></i>
            <span>数据分析</span>
          </template>
          <el-menu-item index="/question-analytics">题目分析</el-menu-item>
          <el-menu-item index="/user-analytics">用户学习情况</el-menu-item>
        </el-submenu>
        
        <el-menu-item index="/settings">
          <i class="el-icon-setting"></i>
          <span>系统设置</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <!-- 主内容区域 -->
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item v-for="item in breadcrumbs" :key="item.path" :to="item.path">
              {{ item.name }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <i class="el-icon-user"></i>
              {{ username || '管理员' }} <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="profile">个人信息</el-dropdown-item>
              <el-dropdown-item command="password">修改密码</el-dropdown-item>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
    
    <!-- 修改密码对话框 -->
    <ChangePasswordDialog :visible.sync="passwordDialogVisible" />
  </el-container>
</template>

<script>
import ChangePasswordDialog from './ChangePasswordDialog.vue'

export default {
  name: 'AdminLayout',
  components: {
    ChangePasswordDialog
  },
  data() {
    return {
      passwordDialogVisible: false
    }
  },
  computed: {
    breadcrumbs() {
      const route = this.$route;
      const breadcrumbs = [{ path: '/', name: '首页' }];
      
      if (route.path !== '/') {
        const pathMap = {
          '/questions': '题目管理',
          '/students': '学员管理',
          '/admins': '管理员管理',
          '/question-analytics': '题目分析',
          '/user-analytics': '用户学习情况',
          '/settings': '系统设置'
        };
        
        if (pathMap[route.path]) {
          breadcrumbs.push({ path: route.path, name: pathMap[route.path] });
        }
      }
      
      return breadcrumbs;
    },
    username() {
      return this.$store.getters.username;
    }
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case 'profile':
          this.$message.info('个人信息功能开发中...');
          break;
        case 'password':
          this.passwordDialogVisible = true;
          break;
        case 'logout':
          this.handleLogout();
          break;
      }
    },
    handleLogout() {
      this.$confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 清除登录状态
        this.$store.dispatch('logout');
        
        // 跳转到登录页
        this.$router.push('/login');
        
        this.$message.success('已退出登录');
      }).catch(() => {
        // 用户取消
      });
    }
  }
}
</script>

<style scoped>
.app-container {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  color: #bfcbd9;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #2b2f3a;
  color: #fff;
}

.logo h2 {
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  border: none;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  cursor: pointer;
  color: #606266;
}

.user-info:hover {
  color: #409EFF;
}

.main-content {
  background-color: #f0f2f5;
  padding: 0;
  overflow: auto;
}
</style>
